# encoding=utf8
import pymysql,requests,urllib3,datetime,re
from lxml import etree
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

'''''MySQL配置'''
host = '127.0.0.1'
users = 'root'
pwd = '123123'
port = 3306
dbs = 'ans'
S = requests.Session()
def mysql(xx):
    value = ((xx))
    db = pymysql.connect(host=host, user=users,passwd=pwd,port=port, db=dbs)
    cursor = db.cursor()
    sql = "INSERT INTO ZZ(xx) values(%s,%s,%s,%s)"
    try:
        cursor.execute(sql, value)
        db.commit()
        print('success!')
    except Exception as e:
        db.rollback()
        print("error.", e)
    db.close()

def down(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37',
        'Cookie':'SESSIONID=HHssJHxj5s6Wm83GRG2WZ2nweTN8CdKJlMDu2Bg6aCO; JOID=VFAdBU4XLCBDsWv8bReBv060lf12XhtgF4EokVZ-YXMpwhDLGDb23CG8ZfJgxfiKbWOcEmSunPKlsHpEtSxTOHI=; osd=U14RBUsQIixDtGzyYReEuEC4lfhxUBdgEoYmnVZ7Zn0lwhXMFjr22SayafJlwvaGbWabHGiumfWrvHpBsiJfOHc=; __snaker__id=9qPGaxdOgA545tOZ; SESSIONID=hAPj4ur8xoljo4uRWToqOibSc9HpLCnOD6UhkydhqYC; JOID=UFwTCkq9wU807-9QErZk2jrgGlkDmOBjH8_KcT6d5G4YxM91M0wXuF3q610VGYJKJsYET1chi2yPy8ZUYeDL9Xo=; osd=UlsXBEK_xks65-1XFrhs2D3kFFEBn-RtF83NdTCV5mkcysd3NEgZsF_t71MdG4VOKM4GSFMvg26Iz8hcY-fP-3I=; _zap=0293cf65-cdb0-479c-9a4e-dec5bb8d2bff; d_c0="ANARmQT-DBWPTk0GhU5PxPRLb9fYFo865JI=|1654429148"; _9755xjdesxxd_=32; YD00517437729195%3AWM_TID=jcZB7OY4yGNAFAERREfEQh8LmNc6OFWX; gdxidpyhxdE=vXz5VnAqvRpjw0xLB03%2F%2BP4T42Cd7Gbb4yx88dItuPGtHsHG7gOT0XUYVPy%2F%2B555vDYE6%5CbrqWbIzBvsGI7JAxlUr9qldboqteZ9mr%2BXqkjOi1tIbhOVzagwEoPrwyTLgkBjmrRLAxdyYxU%2FQigy%2FDKLg8sPnID81pDh%5COLg5kUmuyIW%3A1656346954930; YD00517437729195%3AWM_NI=N85%2FlrwFYRxLd5c%2BQjcABTJTYRw9jbOdkQ7NVrbdpOj0Q2Qudp%2BMU8vHp4bDugt49DEIYkeXLBKr2ctEs1XY4TyuDhIy9iiZw8xb681yj6ss%2F1q3VdAJQSGybe5yTYNcVzg%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eed6bc6597979c89d8748ca88fa3d44a839b8bacd55fb4b9a2ccd98082afbfa9c62af0fea7c3b92a8ee8fbd0ef7ab59a97b3fb48a8ebadccd462a19f8eb5c9609b8eb9dae96a8dbca5b2b64696aeae85f345aaf09fd3d47fa8b0e5aed834b698c0d5e150b7b9bcd5bc508797978ce854909e8593b169b89bbdd2db53f88cb796c7648ba7f78aec52b5e8b883b534f3a7a6d7f279edefbfd4d57083969fb4c673a2bbfb9ad7538ebd9a9be637e2a3; __snaker__id=YTZRAVFeKeABVn0r; z_c0=2|1:0|10:1656346677|4:z_c0|92:Mi4xUVZwWkVnQUFBQUFBMEJHWkJQNE1GU1lBQUFCZ0FsVk5OU2FuWXdEODBtdzBzR2xDRjlKVHdCZEUwX1ZoanJTZEhB|f586c232a96128f13d431fe3d514f4c244cf8ad0b8970937c17a48b6d69bc376; q_c1=3390c02753d74416a1b0dc0d3a3848a4|1656346677000|1656346677000; _xsrf=b3f78b01-15db-419b-85bd-d42f1fa47226; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1658199465,1659152691,1659496489,1659670748; NOT_UNREGISTER_WAITING=1; SESSIONID=G7NW3Mm5jCSdpx9uDfS9C4Cepozy0dailSOxerHjBLD; JOID=W1kRBk3UDlMzpYEfJ9ipzzSvcxQ_mzwcZ5nEdxOxRQVW1vYuVwzboF2ijBslWyFSjM-kNQs7VvXcVZPhzQc1as8=; osd=WlATCknVB1E_oYAWJdStzj2tfxA-kj4QY5jNdR-1RAxU2vIvXg7XpFyrjhchWihQgMulPAk3UvTVV5_lzA43Zss=; tst=r; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1660015662; KLBRSID=2854b34cf0e157055692eeeaeb4361fc|1660015803|1660015057',
    }
    res = S.get(url=url,headers=headers)

    return res



def func(res):
    tree = etree.HTML(res)
    content = ''.join(tree.xpath('//*[@id="root"]/div/main/div/div/div[3]/div[1]/div/div[2]/div/div/div/div[2]/span[1]/div/span//text()')).strip()
    print(content)

if __name__ == "__main__":
    url = 'https://www.zhihu.com/question/518460280/answer/2503891372'
    res = down(url).text
    # print(res)
    func(res)

